package robocup.component.tactics;

import robocup.component.actionfactories.BasicActionFactory;
import robocup.component.geometry.Vektor;

public class StartPositionState
    extends AbstractState {

  private Vektor posKickOffOther = null;

  private Vektor posKickOffOwn   = null;

  /**
   * The state to beam to the start-position!
   * 
   * @param parent -
   *          the StateEvaluation (parent) object
   */
  public StartPositionState(
      final StateEvaluation parent) {

    super(parent);
    this.state = STATES.START_POSITION;
  }

  /**
   * The precondition consits if the correct playmode and if the player is near
   * the position, where he wants to beam to.
   */
  @Override
  public boolean preCondition() {

    this.posKickOffOther = DefaultPositions.getStartPosition(
    		getWorld().getSelfRef().getDefaultRole(), 
    		false);
    this.posKickOffOwn = DefaultPositions.getStartPosition(
    		getWorld().getSelfRef().getDefaultRole(), 
    		true);

    if (!this.getWorld().hasMoved() && this.checkPlayMode()) {
      return true;
    }

    Vektor nearPos;
    if (this.getWorld().isKickOffOwn()) {
      nearPos = this.posKickOffOwn;
    }
    else {
      nearPos = this.posKickOffOther;
    }
    final Vektor myPos = this.getWorld().getSelfRef().getPosition().cloned();
    boolean near = myPos.getDistance(nearPos) < 2.0;
    if (myPos.x == 0 && myPos.y == 0) {
      near = false;
    }
    return (this.checkPlayMode() && !near);
  }

  /**
   * Success is always given in the given Play_Modes!
   */
  @Override
  protected double successProbability() {

    return 1;
  }

  /**
   * Benefit is also maximum, there is no better place!
   */
  @Override
  protected double successBenefit() {

    return 1;
  }

  @Override
  public void calculateMainAction() {

    if (this.getWorld().isKickOffOwn()) {
      this.mainAction = BasicActionFactory.move(this.posKickOffOwn);
    }
    else {
      this.mainAction = BasicActionFactory.move(this.posKickOffOther);
    }
  }
}
